<template>
  <view>
    <my-search @click="gotoSearch"></my-search>
    <view class="scroll-view-container">
      <!-- 左侧的滑动区域 -->
      <scroll-view class="left-scroll-view" scroll-y="true" :style="{height: wh + 'px'}">
        <block v-for="(item,index) in cateList" :key="item.cat_id">
          <view :class="['left-scroll-view-item',index===active?'active':'']" @click="activeChange(index)">
            {{item.cat_name }}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧的滑动区域 -->
      <scroll-view class="right-scroll-view" scroll-y="true" :style="{height: wh + 'px'}" :scroll-top="scrollTop">
        <!-- 二级的渲染 -->
        <view v-for="(item,index) in cateLevel2" :key="index">
          <view class="catelevel2-item">/{{item.cat_name}}/</view>
          <!-- 三级的渲染 -->
          <view class="list3">
            <view class="list3-item" v-for="(item3,index3) in item.children" :key="index3"
              @click="gotoGoodsList(item3)">
              <image :src="item3.cat_icon"></image>
              <text>{{item3.cat_name}}</text>
            </view>
          </view>

        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    mixins: [badgeMix],
    data() {
      return {
        wh: 0,
        // 左边滑动列表数据
        cateList: [],
        active: 0,
        // 二级数据列表
        cateLevel2: [],
        // 滚动条距离上方的位置
        scrollTop: 0
      };
    },
    onLoad() {
      const systemInfo = uni.getSystemInfoSync()
      console.log(systemInfo)
      this.wh = systemInfo.windowHeight - 50
      this.getCateList()
      this.getCateLevel2()
    },
    methods: {
      async getCateList() {
        const res = await uni.$http.get('/api/public/v1/categories')
        console.log('商品列表------', res)
        if (res.data.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.data.message
      },
      activeChange(index) {
        this.active = index
        // 给2级列表重新赋值
        this.cateLevel2 = this.cateList[index].children
        // this.scrollTop = 1
        this.scrollTop = this.scrollTop === 0 ? 1 : 0
      },
      async getCateLevel2() {
        const res = await uni.$http.get('/api/public/v1/categories')
        console.log('商品列表----2级------', res)
        if (res.data.meta.status !== 200) return uni.$showMsg()
        this.cateLevel2 = res.data.message[0].children
      },
      gotoGoodsList(item3) {
        console.log('item3-------', item3)
        uni.navigateTo({
          url: '/subpkg/goods-list/goods-list?cid=' + item3.cat_id
        })
      },
      gotoSearch() {
        uni.navigateTo({
          url: "/subpkg/search/search"
        })
      }
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .left-scroll-view {
      width: 30%;

      .left-scroll-view-item {
        line-height: 60px;
        background-color: #f7f7f7;
        text-align: center;
        font-size: 12px;

        &.active {
          background-color: aliceblue;
          position: relative;

          &:before {
            content: ' ';
            display: block;
            width: 3px;
            height: 30px;
            background-color: #c00000;
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
          }
        }
      }
    }

    .right-scroll-view {
      background-color: white;

      .catelevel2-item {
        font-size: 12px;
        font-weight: bold;
        text-align: center;
        padding: 15px 0;
      }

      .list3 {
        // 让照片横过来
        display: flex;
        // 自动换行
        flex-wrap: wrap;

        .list3-item {
          width: 33.3%;
          margin-bottom: 10px;
          display: flex;
          flex-direction: column;
          align-items: center;
          // background-color: aqua;

          image {
            width: 60px;
            height: 60px;

          }

          text {
            font-size: 12px;
            padding-top: 10px;
          }
        }
      }
    }
  }
</style>
